from PIL import Image
import os

def convert_to_grayscale(input_folder, output_folder):
    # 确保输出文件夹存在
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    # 获取输入文件夹中的所有图片文件
    image_files = [f for f in os.listdir(input_folder) if f.endswith(('.jpg', '.jpeg', '.png', '.bmp'))]

    for image_file in image_files:
        # 读取图像
        image_path = os.path.join(input_folder, image_file)
        image = Image.open(image_path)

        # 转换为灰度色彩模式
        grayscale_image = image.convert("L")

        # 写入灰度图像
        output_path = os.path.join(output_folder, image_file)
        grayscale_image.save(output_path)

        print(f"{image_file} 转换完成")

# 指定输入文件夹和输出文件夹
input_folder = "/home/jolly/develop/PycharmProjects/CorsicanFire/CorsicanFire/val/label1"
output_folder = "/home/jolly/develop/PycharmProjects/CorsicanFire/CorsicanFire/val/label2"

# 执行转换
convert_to_grayscale(input_folder, output_folder)